import React, { useState, useEffect, Fragment } from "react";
// import { Layout, Breadcrumb, Spin } from "antd";
import {
  BrowserRouter as Router,
  // Switch,
  // Route,
  // Link,
  // useLocation,
} from "react-router-dom";
// import { TransitionGroup, CSSTransition } from "react-transition-group";

// import { MenuUnfoldOutlined, MenuFoldOutlined } from "@ant-design/icons";
// import logo from './logo.svg';
import "./App.css";

// import TopHeader from "./components/topHeader/topHeader";
// import LeftSider from "./components/leftSider/leftSider";
// import routes from "./routes";

import useStore from "./utils/myStore";

import Login from "./views/login/login";
import request from "./utils/request";
import { api } from "./utils/constants";

import { isMobile } from "./utils/funtions";
import { Mobile } from "./views/mobile/mobile";
import Main from "./components/main";
import { Route } from "react-router";
import Loading from "./views/loading";
import { message } from "antd";

// const { Header, Footer, Sider, Content } = Layout;

function App(props) {
  const {
    // currentPath,
    userData,
    isLogin,
    // loading,
    // collapsed,
    login,
    // logout,
    // toggle,
    // showLoading,
    // hideLoading,
  } = useStore();

  // console.log(currentPath);

  // const [collapsed, setCollpased] = useState(false);

  // const toggle = () => {
  //   setCollpased(!collapsed);
  // };
  // let router = useRouteMatch();

  // console.log(router);

  // let location = useLocation();

  // useEffect(() => {
  //   if (userData.nickName)
  //     request
  //       .post(api.loginCheck, {})
  //       .then((res) => {
  //         if (res.status === 0) {
  //           login(res.data);
  //         }
  //       })
  //       .catch((error) => {
  //         login({})
  //       });
  // }, [currentPath]);
  // console.log("userData", userData);

  const [data, setData] = useState(false);

  useEffect(() => {

    const loginCheck = async () => {
      let res = await request.post(api.loginCheck, {});
      console.log(res);

      if(!res) {
        setData(true)
        // message.error('网络异常')
        return
      }

      if (userData.nickName) return;
      if (res.status === 0) {
        login(res.data);
      } else {
        login({});
      }

      setData(true);
    };

      loginCheck();

  }, []);

  return (
    <Fragment>
      {isMobile() ? (
        <Mobile />
      ) : data ? (
        !userData.nickName ? (
          <Login />
        ) : (
          <Router>
            <Main />
          </Router>
        )
      ) : (
        // <div>Loading...</div>
        <Loading />
      )}
    </Fragment>
  );
}

export default App;
